/**
 * @Title: UserAction.java
 * @Package ams.action
 * @Description: TODO
 * Copyright: Copyright (c) 2015 
 * 
 * @author lixp
 * @date 2016年3月20日 下午12:28:57
 * @version V1.0
 */

package ams.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;

import ams.model.dto.DataGrid;
import ams.model.entity.Role;
import ams.model.entity.User;
import ams.service.IUserService;
import ams.util.SysUtil;

/**
  * @ClassName: UserAction
  * @Description: TODO
  * @author lixp
  * @date 2016年3月20日 下午12:28:57
  *
  */

@Namespace("/user")
public class UserAction extends BaseAction<User>implements ModelDriven<User> {

	
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private IUserService userService;
	
	private User user = new User();
	
	private List<Role> roleList = new ArrayList<>();
	
	@Action(value="userAdmin",results={@Result(location="/WEB-INF/jsp/user.jsp")})
	public String userAdmin(){
		
		return SUCCESS;
	}
	
	
	@Action(value="addOrUpdate",results={
			@Result(type="json",params={ "root", "execResult", "excludeNullProperties", "true", "contentType", "text/html" })})
	public String addOrUpdate(){
		//id为空表示添加
		if(user.getId() == null){
			super.execResult = userService.add(user);
		}else{
			super.execResult = userService.update(user);
		}
		return SUCCESS;
	}
	
	@Action(value="delete",results={
			@Result(type="json",params={ "root", "execResult", "excludeNullProperties", "true" })})
	public String delete(){
		List<Integer> idList = new ArrayList<>();
		String[] idStrArray = StringUtils.split(ids, ",");
		idList = SysUtil.strArray2IntList(idStrArray);
		execResult = userService.delete(idList);
		return SUCCESS;
	}
	
	@Action(value="dataGrid",results={
			@Result(type="json",params={ "root", "dataGrid", "excludeNullProperties", "true" })})
	public String dataGrid(){
		Long totalCount = userService.totalCount();
		List<User> userList = userService.userPage(page, rows);
		super.setDataGrid(new DataGrid<>(totalCount, userList));
		return SUCCESS;
	}
	
	@Action(value="getUserRole",results={
			@Result(type="json",params={ "root", "roleList", "excludeNullProperties", "true" })})
	public String getUserRole(){
		roleList = userService.getUserRole(user.getId());
		return SUCCESS;
	}
	@Action(value="setUserRole",results={
			@Result(type="json",params={ "root", "execResult", "excludeNullProperties", "true" })})
	public String setUserRole(){
		List<Integer> roleidList = new ArrayList<>();
		String[] roleidStrArray = StringUtils.split(ids, ",");
		roleidList = SysUtil.strArray2IntList(roleidStrArray);
		execResult = userService.setUserRole(user.getId(), roleidList);
		return SUCCESS;
	}
	
	
	
	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	@Override
	public User getModel() {
		return user;
	}

}
